var input = undefined;

function choOnchange() {
    var re = this.value;
    choice.style.display = "none";
    if (re == "二维向量") {
        tow.style.display = "block";
        enlargeAxes(mycanvas, context, 2);
        show.onclick = onBuTow;
        input = tow.getElementsByTagName("input");
        for (let i = 0; i <= 3; i++) {
            input[i].onmouseover = hint;
        }
    } else if (re == "三维向量") {
        thiree.style.display = "block";
        enlargeAxes(mycanvas, context, 3);
        show.onclick = onBuThiree;
        input = thiree.getElementsByTagName("input");
        for (let i = 0; i <= 8; i++) {
            input[i].onmouseover = hint;
        }
    }
}

cho[0].onclick = choOnchange;
cho[1].onclick = choOnchange;

/*
 *获取帮助参数
 */
function getHelpNumber(x, y, z, l) {
    let e1 = Math.atan2(y, x); //x与y夹角
    let E1 = Math.atan2(y, z); //y与z夹角
    let e11 = 0; //辅助角（xy）
    let E11 = 0; //辅助角（yz）
    let ij = 0; //起始点
    let he11 = 0; //xy夹角10等分角
    let hE11 = 0; //yz夹角10等分角


    if (l == 'i') {
        if (x >= 0 && y >= 0 && z >= 0) //1
        {
            ij = 1;
            e11 = 0;
            E11 = pi / 2;
            he11 = e1 / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x < 0 && y >= 0 && z >= 0) //2
        {
            ij = -1;
            e11 = pi;
            E11 = pi / 2;
            he11 = (e1 - pi) / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x < 0 && y < 0 && z >= 0) //3
        {
            ij = -1;
            e11 = -pi;
            E11 = pi / 2;
            he11 = (e1 + pi) / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x >= 0 && y < 0 && z >= 0) //4
        {
            ij = 1;
            e11 = 0;
            E11 = pi / 2;
            he11 = e1 / 10;
            hE11 = (E1 - pi / 2) / 10;

        } else if (x >= 0 && y >= 0 && z < 0) //5
        {
            ij = 1;
            e11 = 0;
            E11 = 0;
            he11 = e1 / 10;
            hE11 = E1 / 10;
        } else if (x < 0 && y >= 0 && z < 0) //6
        {
            ij = -1;
            e11 = 0;
            he11 = e1 / 10;
            E11 = 0;
            hE11 = E1 / 10;

        } else if (x < 0 && y < 0 && z < 0) //7
        {
            ij = -1;
            e11 = -pi;
            he11 = (pi + e1) / 10;
            E1 = 0;
            hE11 = E1 / 10;

        } else if (x >= 0 && y < 0 && z < 0) //8
        {
            ij = 1;
            e11 = 0;
            he11 = e1 / 10;
            E1 = 0;
            hE11 = E1 / 10;
        }
    } else if (l == 'j') {
        if (x >= 0 && y >= 0 && z >= 0) //1
        {
            ij = 1;
            e11 = pi / 2;
            E11 = pi / 2;
            he11 = (e1 - pi / 2) / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x < 0 && y >= 0 && z >= 0) //2
        {
            ij = 1;
            e11 = pi / 2;
            E11 = pi / 2;
            he11 = (e1 - pi / 2) / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x < 0 && y < 0 && z >= 0) //3
        {
            ij = -1;
            e11 = -pi / 2;
            E11 = pi / 2;
            he11 = (e1 + pi / 2) / 10;
            hE11 = (E1 - pi / 2) / 10;
        } else if (x >= 0 && y < 0 && z >= 0) //4
        {
            ij = -1;
            e11 = -pi / 2;
            E11 = pi / 2;
            he11 = (e1 + pi / 2) / 10;
            hE11 = (E1 - pi / 2) / 10;

        } else if (x >= 0 && y >= 0 && z < 0) //5
        {
            ij = 1;
            e11 = pi / 2;
            E11 = 0;
            he11 = (e1 - pi / 2) / 10;
            hE11 = E1 / 10;
        } else if (x < 0 && y >= 0 && z < 0) //6
        {
            ij = 1;
            e11 = pi / 2;
            E11 = 0;
            he11 = (e1 - pi / 2) / 10;
            hE11 = E1 / 10;

        } else if (x < 0 && y < 0 && z < 0) //7
        {
            ij = -1;
            e11 = -pi / 2;
            E11 = 0;
            he11 = (e1 + pi / 2) / 10;
            hE11 = E1 / 10;

        } else if (x >= 0 && y < 0 && z < 0) //8
        {
            ij = -1;
            e11 = -pi / 2;
            E11 = 0;
            he11 = (e1 + pi / 2) / 10;
            hE11 = E1 / 10;
        }

    } else if (l == 'k') {
        if (x >= 0 && y >= 0 && z >= 0) //1
        {
            ij = 1;
            e11 = 0;
            E11 = 0;
            he11 = e1 / 10;
            hE11 = E1 / 10;
        } else if (x < 0 && y >= 0 && z >= 0) //2
        {
            ij = 1;
            e11 = 0;
            E11 = 0;
            he11 = e1 / 10;
            hE11 = E1 / 10;
        } else if (x < 0 && y < 0 && z >= 0) //3
        {
            ij = 1;
            e11 = 0;
            E11 = 0;
            he11 = e1 / 10;
            hE11 = E1 / 10;
        } else if (x >= 0 && y < 0 && z >= 0) //4
        {
            ij = 1;
            e11 = 0;
            E11 = 0;
            he11 = e1 / 10;
            hE11 = E1 / 10;

        } else if (x >= 0 && y >= 0 && z < 0) //5
        {
            ij = -1;
            e11 = 0;
            E11 = pi;
            he11 = e1 / 10;
            hE11 = (E1 - pi) / 10;
        } else if (x < 0 && y >= 0 && z < 0) //6
        {
            ij = -1;
            e11 = 0;
            E11 = pi;
            he11 = e1 / 10;
            hE11 = (E1 - pi) / 10;

        } else if (x < 0 && y < 0 && z < 0) //7
        {
            ij = -1;
            e11 = 0;
            E11 = pi;
            he11 = e1 / 10;
            hE11 = (E1 - pi) / 10;

        } else if (x >= 0 && y < 0 && z < 0) //8
        {
            ij = -1;
            e11 = 0;
            E11 = pi;
            he11 = e1 / 10;
            hE11 = (E1 - pi) / 10;
        }
    }

    return {
        me11: e11,
        mE11: E11,
        mijk: ij,
        mhe11: he11,
        mhE11: hE11,
        me1: e1,
        mE1: E1
    }

}


async function onBuTow() {
    enlargeAxes(mycanvas, context, 2);

    var i1 = input[0].value;
    var j1 = input[1].value;
    var i2 = input[2].value;
    var j2 = input[3].value;

    if (i1 == "" || j1 == "" || i2 == "" || j2 == "") {
        alert("请先输入完整！");
        return;
    }

    try {
        var re1 = eval("(" + i1 + ")*(" + i2 + ")+(" + j1 + ")*(" + j2 + ")") * 1; //(i1*i2+j1*j2)=0

        var re2 = eval("(" + i1 + ")*(" + i1 + ")+(" + j1 + ")*(" + j1 + ")"); //|(i1,j1)|=1

        var re3 = eval("(" + i2 + ")*(" + i2 + ")+(" + j2 + ")*(" + j2 + ")"); //|(i2,j2)|=1

        if (Math.abs(re1) > 0.0001 || Math.abs(re2 - 1) > 0.0001 || Math.abs(re3 - 1) > 0.0001) {
            alert("非标准正交基");
            return;
        }
    } catch (error) {
        alert("格式不正确，请检查输入！");
        return;
    }

    var i11 = eval(i1) * 1;
    var j11 = eval(j1) * 1;
    var i22 = eval(i2) * 1;
    var j22 = eval(j2) * 1;

    var i = 1; //i起点
    var vi = 0; //i轴起点角度
    var j = 1; //j起点
    var vj = 0; //j轴起点角度
    var e1 = Math.atan2(j11, i11); //(i1,j1)旋转角度
    var e2 = Math.atan2(j22, i22); //(i2,j2)旋转角度
    var hij1 = 0; //(i,0)到(i1,j1)分10等分，每等分大小
    var hij2 = 0; //(0,j)到(i2,j2)分10等分，每登分大小

    if (i11 >= 0 && j11 >= 0) { //(i1,j1)一象限

        i = 1;
        vi = 0;
        hij1 = e1 / 10;

        if (i22 >= 0 && j22 <= 0) { // (i2,j2)四象限
            j = -1;
            vj = -pi / 2;
            hij2 = (e2 + pi / 2) / 10;
        } else if (i22 <= 0 && j22 >= 0) { //(i2,j2)二象限
            j = 1;
            vj = pi / 2;
            hij2 = (e2 - pi / 2) / 10;

        } else {
            alert("非标准正交基");
            return;
        }

    } else if (i11 >= 0 && j11 < 0) { //(i1,j1)四象限 

        i = 1;
        vi = 0;
        hij1 = e1 / 10;

        if (i22 >= 0 && j22 >= 0) { //(i2,j2)一象限
            j = 1;
            vj = pi / 2;
            hij2 = (e2 - pi / 2) / 10;
        } else if (i22 < 0 && j22 < 0) { //(i2,j2)三象限
            j = -1;
            vj = -pi / 2;
            hij2 = (e2 + pi / 2) / 10;
        } else {
            alert("非标准正交基");
            return;
        }

    } else if (i11 < 0 && j11 >= 0) { //(i1,j1)二象限

        i = -1;
        vi = pi;
        hij1 = (e1 - pi) / 10;

        if (i22 >= 0 && j22 >= 0) { //(i2,j2)一象限
            j = 1;
            vj = pi / 2;
            hij2 = (e2 - pi / 2) / 10;

        } else if (i22 < 0 && j22 < 0) { //(i2,j2)三象限
            j = -1;
            vj = -pi / 2;
            hij2 = (e2 + pi / 2) / 10;
        } else {
            alert("非标准正交基");
            return;
        }

    } else if (i11 < 0 && j11 < 0) { //(i1,j1)三象限

        i = -1;
        vi = -pi;
        hij1 = (e1 + pi) / 10;

        if (i22 <= 0 && j22 >= 0) { //(i2,j2)二象限
            j = 1;
            vj = pi / 2;
            hij2 = (e2 - pi / 2) / 10;

        } else if (i22 >= 0 && j22 < 0) { //(i2,j2)四象限
            j = -1;
            vj = -pi / 2;
            hij2 = (e2 + pi / 2) / 10;
        } else {
            alert("非标准正交基");
            return;
        }
    }

    context.save();

    drowVector(context, i, 0, "red");
    drowVector(context, 0, j, "blue");
    context.fillStyle = "red";
    context.fillText('i', i * c1, 5);
    context.fillStyle = "blue";
    context.fillText('j', -5, -j * c1);
    context.restore();
    await Sleep(3000);

    for (var a = 1; a <= 10; a++) {
        enlargeAxes(mycanvas, context, 2);
        drowVector(context, Math.cos(vi + hij1 * a), Math.sin(vi + hij1 * a), "red");
        drowVector(context, Math.cos(vj + hij2 * a), Math.sin(vj + hij2 * a), "blue");
        await Sleep(100);
    }
    context.save();
    context.fillStyle = "red";
    context.fillText('i', cos(e1) * c1, -sin(e1) * c1);
    context.fillStyle = "blue";
    context.fillText('j', cos(e2) * c1, -sin(e2) * c1);
    context.restore();
}

async function onBuThiree() {
    var i1 = input[0].value;
    var j1 = input[1].value;
    var k1 = input[2].value;
    var i2 = input[3].value;
    var j2 = input[4].value;
    var k2 = input[5].value;
    var i3 = input[6].value;
    var j3 = input[7].value;
    var k3 = input[8].value

    if (i1 == "" || j1 == "" || k1 == "" ||
        i2 == "" || j2 == "" || k2 == "" ||
        i3 == "" || j3 == "" || k3 == "") {
        alert("请输入完整！");
        return;
    }

    try {

        /*
         *(i1,j1,k1)x(i2,j2,k2) = y(i3,j3,k3)
         *
         *(j1*k2-k1*j2)/i3 == (k1*i2-i1*k2)/j3 == (i1*j2-j1*i2)/k3
         */
        var re11 = eval("((" + j1 + ")*(" + k2 + ")-(" + k1 + ")*(" + j2 + "))/(" + i3 + ")");

        var re12 = eval("((" + k1 + ")*(" + i2 + ")-(" + i1 + ")*(" + k2 + "))/(" + j3 + ")");

        var re13 = eval("((" + i1 + ")*(" + j2 + ")-(" + j1 + ")*(" + i2 + "))/(" + k3 + ")");

        var re2 = eval(i1 + "*(" + i1 + ")+(" + j1 + ")*(" + j1 + ")+(" + k1 + ")*(" + k1 + ")"); //|(i1,j1,k1)|=1

        var re3 = eval(i2 + "*(" + i2 + ")+(" + j2 + ")*(" + j2 + ")+(" + k2 + ")*(" + k2 + ")"); //|(i2,j2,k2)|=1

        var re4 = eval(i3 + "*(" + i3 + ")+(" + j3 + ")*(" + j3 + ")+(" + k3 + ")*(" + k3 + ")"); //|(i2,j2,k3)|=1

        if (Math.abs(re11 - re12) > 0.0001 || //检查是否等比例
            Math.abs(re12 - re13) > 0.0001 || // 检查是否等比例
            Math.abs(re2 - 1) > 0.0001 || //检查模是否等于1
            Math.abs(re3 - 1) > 0.0001 || //检查模是否等于1
            Math.abs(re4 - 1) > 0.0001) { //检查模是否等于1
            alert("非标准正交基");
            return;
        }
    } catch (error) {
        alert("输入格式不正确，或非标准正交基，请重新输入！");
        return;
    }
    var i11 = eval(i1) * 1;
    var j11 = eval(j1) * 1;
    var k11 = eval(k1) * 1;
    var i22 = eval(i2) * 1;
    var j22 = eval(j2) * 1;
    var k22 = eval(k2) * 1;
    var i33 = eval(i3) * 1;
    var j33 = eval(j3) * 1;
    var k33 = eval(k3) * 1;
    var i = 0;
    var j = 0;
    var k = 0;

    /*
     *辅助角
     */
    var e11 = 0;
    var E11 = 0;
    var e22 = 0;
    var E22 = 0;
    var e33 = 0;
    var E33 = 0;

    /*
     *10等分角
     */
    var he11 = 0;
    var hE11 = 0;
    var he22 = 0;
    var hE22 = 0;
    var he33 = 0;
    var hE33 = 0;

    var rei = getHelpNumber(i11, j11, k11, 'i');
    var rej = getHelpNumber(i22, j22, k22, 'j');
    var rek = getHelpNumber(i33, j33, k33, 'k');

    i = rei['mijk'];
    e11 = rei['me11'];
    E11 = rei['mE11'];
    he11 = rei['mhe11'];
    hE11 = rei['mhE11'];

    j = rej['mijk'];
    e22 = rej['me11'];
    E22 = rej['mE11'];
    he22 = rej['mhe11'];
    hE22 = rej['mhE11'];

    k = rek['mijk'];
    e33 = rek['me11'];
    E33 = rek['mE11'];
    he33 = rek['mhe11'];
    hE33 = rek['mhE11'];

    drow3DVector3(context, i, 0, 0, "red");
    drow3DVector3(context, 0, j, 0, "green");
    drow3DVector3(context, 0, 0, k, "blue");
    addFont_3D(context, "i", i, 0, 0, "red", "5pt Times new roman");
    addFont_3D(context, "j", 0, j, 0, "green", "5pt Times new roman");
    addFont_3D(context, "k", 0, 0, k, "blue", "5pt Times new roman");
    await Sleep(3000);
    for (var a = 1; a <= 10; a++) {
        enlargeAxes(mycanvas, context, 3);
        drow3DVector3(context, sin(E11 + hE11 * a) * cos(e11 + he11 * a), sin(E11 + hE11 * a) * sin(e11 + he11 * a), cos(E11 + hE11 * a), "red");
        drow3DVector3(context, sin(E22 + hE22 * a) * cos(e22 + he22 * a), sin(E22 + hE22 * a) * sin(e22 + he22 * a), cos(E22 + hE22 * a), "green");
        drow3DVector3(context, sin(E33 + hE33 * a) * cos(e33 + he33 * a), sin(E33 + hE33 * a) * sin(e33 + he33 * a), cos(E33 + hE33 * a), "blue");
        await Sleep(100);
    }
}